﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Interactivity;
using nRoute.Components;
#if (WINRT)
using Windows.UI.Xaml;
using nRoute.Behaviors.Interactivity;
using Windows.UI.Xaml.Data;
#endif

namespace nRoute.Behaviors
{
    public class BridgeValueBehavior
#if (WINRT)
        : Behavior<FrameworkElement>
#else
        : Behavior<DependencyObject>
#endif
    {
        public static readonly DependencyProperty ValueRelayProperty =
            DependencyProperty.Register("ValueRelay", typeof(ValueRelay), typeof(BridgeValueBehavior),
                new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE, OnValueRelayChanged));

        public static readonly DependencyProperty ValueSourceProperty =
            DependencyProperty.Register("ValueSource", typeof(Object), typeof(BridgeValueBehavior),
                new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE, OnValueSourceChanged));

#region Properties

        [Category("Common Properties")]
        public ValueRelay ValueRelay 
        {
            get { return (ValueRelay)GetValue(ValueRelayProperty); }
            set { SetValue(ValueRelayProperty, value); }
        }

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Object ValueSource
        {
            get { return GetValue(ValueSourceProperty); }
            set { SetValue(ValueSourceProperty, value); }
        }

#if (WINRT)

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Binding ValueRelayBinding
        {
            get { return GetBinding(ValueRelayProperty); }
            set { SetBinding<ValueRelay>(ValueRelayProperty, value); }
        }

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Binding ValueSourceBinding
        {
            get { return GetBinding(ValueSourceProperty); }
            set { SetBinding<Object>(ValueSourceProperty, value); }
        }

#endif

#endregion

#region Handlers

        private static void OnValueSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((BridgeValueBehavior)d).UpdateRelay();
        }

        private static void OnValueRelayChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((BridgeValueBehavior)d).UpdateRelay();
        }

#endregion

#region Overrides

        protected override void OnAttached()
        {
            base.OnAttached();
            UpdateRelay();
        }

#endregion

#region Helpers
        
        private void UpdateRelay()
        {
            if (ValueRelay != null && this.ValueSource != null)
            {
                ValueRelay.Value = ValueSource;
            }
        }

#endregion

    }
}
